package is.web.table;

import is.NullUtil;
import is.interfaces.Dao;
import is.interfaces.Table;
import is.model.bean.*;
import is.model.dao.*;
import is.model.exceptions.EmptyValueException;
import is.web.GetParam;
import java.sql.SQLException;

/**
 *
 * @author aspr0
 */
public class DepartmentTable extends AbstractHierarchyTable<Department> implements Table<Department>
{
    @Override
    public String genTable() throws EmptyValueException, SQLException
    {

        String table =
                "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s%s</tbody>"
                + "</table>";
        String header =
                "<tr>"
                + "<td><a href=\".\">id</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=0\">name</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=1\">parent department</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=2\">boss</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=3\">region</a> %s</td>"
                + "<td>tools</td>"
                + "</tr>";
        String id = (hasSearched) ? NullUtil.toString(initialBean.getId()) : "";
        String name = (hasSearched) ? NullUtil.toString(initialBean.getName()) : "";
        String searchForm =
                "<tr>"
                + "<form method=\"post\">"
                + "<td><input type=\"text\" name=\"id\" value=\"" + id + "\" class=\"search-id\"></td>"
                + "<td><input type=\"text\" name=\"name\" value=\"" + name + "\" class=\"search-wide\"></td>"
                + "<td></td>"
                + "<td></td>"
                + "<td></td>"
                + "<td><input type=\"submit\" value=\"search\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";

        switch (orderBy)
        {
            case 0:
                header = String.format(header, "", ARROW_UP, "", "", "");
                break;

            case 1:
                header = String.format(header, "", "", ARROW_UP + "i", "", "");
                break;

            case 2:
                header = String.format(header, "", "", "", ARROW_UP + "i", "");
                break;

            case 3:
                header = String.format(header, "", "", "", "", ARROW_UP + "i");
                break;

            default:
                String hierarchyBuilt = "";
                if (!hasSearched)
                {
                    buildHierarchy();
                    hierarchyBuilt = "h";
                }
                header = String.format(header, ARROW_UP + hierarchyBuilt, "", "", "", "", "");
                break;
        }

        for (Department department : beans)
        {
            Employee initialEmployee = new Employee();
            initialEmployee.setId(department.getBossId());
            Dao<Employee> employeeDao = new EmployeeDao();
            employeeDao.setInitialBean(initialEmployee);
            Employee boss = employeeDao.retrieve();
            String bossLink = NullUtil.toLink("<a href=\"../employee/retrieve.jsp?id=%s\">%s</a>",
                    boss.getId(), boss.getFirstName() + " " + boss.getLastName());


            Region initialRegion = new Region();
            initialRegion.setId(department.getRegionId());
            Dao<Region> regionDao = new RegionDao();
            regionDao.setInitialBean(initialRegion);
            Region region = regionDao.retrieve();
            String regionLink = NullUtil.toLink("<a href=\"../region/retrieve.jsp?id=%s\">%s</a>", region.getId(), region.getName());

            initialBean = new Department();
            initialBean.setId(department.getParentId());
            Dao<Department> departmentDao = new DepartmentDao();
            departmentDao.setInitialBean(initialBean);
            Department parentDepartment = departmentDao.retrieve();
            String parentDepartmentName = NullUtil.concat(parentDepartment.getName());


            String row =
                    "<tr>"
                    + "<td>" + department.getId() + "</td>"
                    + "<td>" + department.getName() + "</td>"
                    + "<td>" + parentDepartmentName + "</td>"
                    + "<td>" + bossLink + "</td>"
                    + "<td>" + regionLink + "</td>"
                    + "<td>"
                    + "<a href=\"retrieve.jsp?id=" + department.getId() + "\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"update.jsp?id=" + department.getId() + "\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"copy.jsp?id=" + department.getId() + "\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"delete.jsp?id=" + department.getId() + "\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;
        }
        return String.format(table, header, searchForm, body);
    }
}
